<?php

/**
 * This is the model class for table "{{book}}".
 *
 * The followings are the available columns in table '{{book}}':
 * @property integer $id
 * @property string $bookname
 * @property string $author
 * @property integer $browsertimes
 * @property string $abstract
 * @property integer $vote
 * @property string $content
 * @property integer $typeid
 * @property integer $price
 * @property string $picture
 * @property string $file
 *
 * The followings are the available model relations:
 * @property Type $type
 * @property Message[] $messages
 * @property Record[] $records
 */
class Book extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{book}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('bookname, author, typeid, price', 'required'),
			array('browsertimes, vote, typeid, price', 'numerical', 'integerOnly'=>true),
			array('bookname, author', 'length', 'max'=>45),
			array('abstract', 'length', 'max'=>500),
            // array('picture', 'file', 'types' => 'jpg,gif,png', 'on'=>'insert'),
            // array('file', 'file', 'types' => 'txt,doc,docx,pdf', 'on'=>'insert'),
			array('content', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, bookname, author, browsertimes, abstract, vote, content, typeid, price, picture, file', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'type' => array(self::BELONGS_TO, 'Type', 'typeid'),
			'messages' => array(self::HAS_MANY, 'Message', 'bookid'),
			'records' => array(self::HAS_MANY, 'Record', 'bookid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'bookname' => '书名',
			'author' => '作者',
			'browsertimes' => '浏览次数',
			'abstract' => '简要',
			'vote' => 'Vote',
			'content' => '内容',
			'typeid' => '类型',
			'price' => '价格',
			'picture' => '图片',
			'file' => '图书文件',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('bookname',$this->bookname,true);
		$criteria->compare('author',$this->author,true);
		$criteria->compare('browsertimes',$this->browsertimes);
		$criteria->compare('abstract',$this->abstract,true);
		$criteria->compare('vote',$this->vote);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('typeid',$this->typeid);
		$criteria->compare('price',$this->price);
		$criteria->compare('picture',$this->picture,true);
		$criteria->compare('file',$this->file,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Book the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    /**
     * 删除记录之前的动作
     *
     */
    public function beforeDelete()
    {
        Message::model()->deleteAll('bookid='.$this->id);
        Record::model()->deleteAll('bookid='.$this->id);
        return true;
    }
}
